package controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.StringTokenizer;

import paper.PubMedDB;
import entity.Protein;
import util.CommonUtil;
import util.FileUtil;
import util.UniprotParserUtil;
import util.UniprotXML;
import PMCHandler.GetPMCRecord;
import DBHandler.PostgreSQLJDBC;
public class ProteinMutationsExtractor {
	
	public static void main(String[] args) {
		String[] inputFilePaths = {
			"proteins/rat.txt"
//			"proteins/drosophila.txt",
//			"proteins/HIV.txt",
//			"proteins/pomble.txt",
//			"proteins/s.cerevis.txt",
//			"proteins/rat.txt",
//			"proteins/arabidopsis.txt",
//			"proteins/human.txt",
//			"proteins/mouse.txt",
		};
		
		for (String filePath: inputFilePaths) {
			String delim = ",^*/+-&=<>=%(){};.";
			String[] strArr=SplitUsingTokenizer(filePath,delim);
			FindMutationPapers(strArr[1],filePath);
		}
		
	}
	public static String[] SplitUsingTokenizer(String subject, String delimiters) {
		   StringTokenizer strTkn = new StringTokenizer(subject, delimiters);
		   ArrayList<String> arrLis = new ArrayList<String>(subject.length());

		   while(strTkn.hasMoreTokens())
		      arrLis.add(strTkn.nextToken());

		   return arrLis.toArray(new String[0]);
		}
	
	public static void FindMutationPapers(String organism,String inputFilePath) {
		String fileName = CommonUtil.getFileNameWithoutExtension(inputFilePath);
		String fileContents = FileUtil.getFileContents(inputFilePath);
		List<Protein> proteins = UniprotParserUtil.parseSearchResults(fileContents);
		UniprotXML uni = new UniprotXML();
		PubMedDB pubmed = new PubMedDB();
		
		pubmed.resetCounter();
		String filesFolder = "files/";
		if(!FileUtil.pathExists(filesFolder)) {
			FileUtil.createDirectory(filesFolder);
		}
		for (Protein p: proteins) {
			try {
				String proteinName = p.getEntry();
				System.out.println(proteinName+"=========");
				uni.downloadXMLfromUniprot(proteinName);
				System.out.println("Mutations");
				Set<String> pubMedIDsMutations = uni.getPubMedIdsForMutations(proteinName);
				
				for(String id : pubMedIDsMutations) {
					System.out.println(id);
					String downloadFolder = filesFolder+fileName+"/"+proteinName+"/"+id;
					String pmcID=GetPMCRecord.pubmed2PMCID(id);
					//save protein and each mutations in the protein along with its PMCId
					PostgreSQLJDBC.saveProtMutPMCIDtoDB(organism,proteinName,id,pmcID);
					if(pmcID!=null && !pmcID.isEmpty())
					{
						if(GetPMCRecord.downloadFullXML(pmcID,fileName,proteinName,id))
						{
							System.out.println("Downloaded Full Text XML");
						}
					}
					//boolean downloaded = pubmed.downloadPaper(id, downloadFolder);
				}
				
//				System.out.println("Locations");
//				Set<String> pubMedIDsLocations = uni.getPubMedIdsForLocations(p.getEntry());
//				for(String id : pubMedIDsLocations){
//					System.out.println(id);
//					pubmed.downloadPaper(id, filesFolder);
//				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		pubmed.printCounterStatistics(filesFolder+"statistics-"+fileName, "statistics-"+fileName);
	}
	
	

}
 